package com.jml.zookeeper.二期.创建节点;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.KeeperException;

/**
 * @author:jml
 * @date: 2019/12/9 10:19 @description：
 */
public class 监听节点变化 {
    // 参数1 连接地址
    private static final String ADDRES = "172.16.5.118:2181";
    // 参数2 zk超时时间
    private static final int TIMEOUT = 5000;
    // 计数器
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZkClient zkClient = new ZkClient(ADDRES, TIMEOUT);
        String path = "/jml-service";
        // 子节点发生变化
        /*zkClient.subscribeChildChanges(path, new IZkChildListener() {
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {
                System.out.println(s + "节点发生了变化,剩余一下节点");
                list.forEach((t) -> System.out.println(t));
            }
        });*/
        //节点发生变化
        zkClient.subscribeDataChanges(path + "/8080", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println(s + "节点内容发生了变化:" + o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println(s + "节点被移除~~~");
            }
        });
        while (true) {
        
        }
    }

}
